<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="赛事名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入赛事名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:event:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:event:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:event:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getData"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="场次" prop="matchId" width="120"/>
      <el-table-column label="赛事名称" prop="name" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="赛事地点" prop="venue" :show-overflow-tooltip="true" width="150"/>
      <el-table-column label="主队/客队" :show-overflow-tooltip="true" width="150">
        <template slot-scope="scope">
          {{scope.row.teamNameA + '/' + scope.row.teamNameB}}
        </template>
      </el-table-column>
      <el-table-column label="主队/客队得分" prop="name" :show-overflow-tooltip="true" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.homeScore }}/{{ scope.row.awayScore }}</span>
        </template>
      </el-table-column>
      <el-table-column label="比赛时间" prop="roleSort" width="100"/>
      <el-table-column label="比赛状态" align="center" width="100">
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createDt" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDt) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:event:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:event:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="赛事名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入赛事名称"/>
        </el-form-item>
        <el-form-item label="比赛地点" prop="venue">
          <el-input v-model="form.venue" placeholder="请输入比赛地点"/>
        </el-form-item>
        <el-form-item label="选择主队" prop="homeTeamId">
          <el-select v-model="form.homeTeamId" placeholder="请选择主队队伍">
            <el-option
              v-for="dict in teamInfo"
              :key="dict.teamId"
              :label="dict.teamName"
              :value="dict.teamId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主队得分" prop="homeScore">
          <el-input-number v-model="form.homeScore" controls-position="right" :min="0"/>
        </el-form-item>
        <el-form-item label="选择客队" prop="awayTeamId">
          <el-select v-model="form.awayTeamId" placeholder="请选择客队队伍">
            <el-option
              v-for="dict in teamInfo"
              :key="dict.teamId"
              :label="dict.teamName"
              :value="dict.teamId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客队得分" prop="awayScore">
          <el-input-number v-model="form.awayScore" controls-position="right" :min="0"/>
        </el-form-item>
        <el-form-item label="比赛时间" prop="matchDate">
          <el-date-picker
            v-model="form.matchDate"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="date"></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {addTeam, delMatch, getList, getTeamInfo, updateTeam} from "@/api/team/team";
import {addPost, delPost, updatePost} from "@/api/system/post";

export default {
  name: "event_info",
  data() {
    return {
      title: '',
      form: {},
      // 显示搜索条件
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
      },
      matchIds: [],
      total: 0,
      size: 10,
      current: 1,
      multipleSelection: [],
      tableData: [],
      teamInfo: [],
      // 是否显示弹出层
      open: false,
      // 遮罩层
      loading: true,
      // 表单校验
      rules: {
        name: [
          {required: true, message: "赛事名称不能为空", trigger: "blur"}
        ],
        venue: [
          {required: true, message: "赛事地点不能为空", trigger: "blur"}
        ],
        homeTeamId: [
          {required: true, message: "主队队伍不能为空", trigger: "blur"}
        ],
        homeScore: [
          {required: true, message: "主队得分不能为空", trigger: "blur"}
        ],
        awayTeamId: [
          {required: true, message: "客队队伍不能为空", trigger: "blur"}
        ],
        awayScore: [
          {required: true, message: "客队得分不能为空", trigger: "blur"}
        ],
        matchDate: [
          {required: true, message: "赛事时间不能为空", trigger: "blur"}
        ]
      }
    }
  },
  created() {
    this.getData();
    this.getTeamInfo();
  },
  methods: {
    /** 删除按钮操作 */
    handleDelete(row) {
      const matchIds = row.matchId || this.matchIds;
      this.$modal.confirm('是否确认删除赛事场次为"' + matchIds + '"的数据项？').then(function() {
        return delMatch(matchIds);
      }).then(() => {
        this.getData();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.matchId != undefined) {
            updateTeam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTeam(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const postId = row.matchId || this.matchIds
      updateTeam(postId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改赛事";
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getData();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.matchIds = selection.map(item => item.matchId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    getData() {
      getList().then(res => {

      });
    },
    //获取队伍列表
    getTeamInfo(){
      getTeamInfo().then(res => {
        if(res.data.data) {
          this.teamInfo = res.data.data;
        } else {
          this.$message.error("查询队伍异常")
        }
      });
    },
    // 表单重置
    reset() {
      this.queryParams.name = '';
      this.resetForm("queryForm");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加赛事";
    },
  }
}
</script>

<style scoped>

</style>
